package me.wangcai.trafficpoliceai.model.entity;


import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import me.wangcai.trafficpoliceai.enums.PermissionEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@ToString
@NoArgsConstructor
public class User implements UserDetails {
    private int id;
    private String username;
    private String password;
    @TableField("carId")
    private String carId;
    private String mail;
    private PermissionEnum permission;

    public User(String username,String password,String mail){
        this.username = username;
        this.password =password;
        this.mail = mail;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        for (int i = 0; i <= permission.getIndex(); i++) {
            list.add(new SimpleGrantedAuthority(PermissionEnum.indexOf(i).name()));
        }
        return list;
    }


    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return false;
    }
}
